package com.icarus.entity;

import com.icarus.util.DataUtil;
import com.icarus.view.MainView;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 敌人飞机
 */
public class Enemy extends Plane implements Runnable {


    /**
     * 0 为 普通 敌人飞机
     *
     * 1 为 boss 飞机
     *
     */
    private int type;


    public Enemy() {
        //设置弹药库
        List<Bullet> bullets = Collections.synchronizedList(new ArrayList<>());
        this.setBullets(bullets);
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    //向上移动
    public void moveUp() {
        //不能飞过边界外
        this.setDirection(0);
        if(this.getY()>(0+this.getSpeed())&&!isTouch()) {
            this.setY(this.getY()-this.getSpeed());
        }
    }

    //向下移动
    public void moveDown() {
        //不能飞过屏幕的三分之一
        this.setDirection(1);
        if(this.getY()<(MainView.HEIGHT/3)&&!isTouch()) {
            this.setY(this.getY()+this.getSpeed());
        }
    }

    //向左移动
    public void moveLeft() {
        //不能飞过边界外
        this.setDirection(2);
        if(this.getX()>(0+this.getSpeed())&&!isTouch()) {
            this.setX(this.getX()-this.getSpeed());
        }
    }

    //向右移动
    public void moveRight() {
        //不能飞过边界外
        this.setDirection(3);
        if(this.getX()<(MainView.WIDTH-this.getIcon().getIconWidth()-this.getSpeed())&&!isTouch()) {
            this.setX(this.getX()+this.getSpeed());
        }
    }



    public boolean isTouch() {
        //获取敌人自己的坐标
        int x = this.getX();
        int y = this.getY();

        //获取敌人宽度和长度
        int width = this.getIcon().getIconWidth();
        int height = this.getIcon().getIconHeight();


        Hero hero = MainView.hero;

        //获取英雄的坐标

        int heroX = hero.getX();
        int heroY = hero.getY();

        int heroWidth = hero.getIcon().getIconWidth();
        int heroHeight = hero.getIcon().getIconHeight();


        switch (this.getDirection()) {
            case 0:
                //向上

                if((y>=heroY&&y<=heroY+heroHeight)&&((x>=heroX&&x<=heroX+heroWidth)||(x+width>=heroX&&x+width<=heroX+heroWidth))) {

                    return true;
                }

                break;
            case 1:
                //向下
                if((y+height>=heroY&&y+height<=heroY+heroHeight)&&((x>=heroX&&x<=heroX+heroWidth)||(x+width>=heroX&&x+width<=heroX+heroWidth))) {


                    return true;
                }

                break;
            case 2:
                //向左

                if((x>=heroX&&x<=heroX+heroWidth)&&((y>=heroY&&y<=heroY+heroHeight)||(y+height>=heroY&&y+height<=heroY+heroHeight))) {
                    return true;
                }

                break;
            case 3:

                //向右
                if((x+width>=heroX&&x+width<=heroX+heroWidth)&&((y>=heroY&&y<=heroY+heroHeight)||(y+height>=heroY&&y+height<=heroY+heroHeight))) {

                    return true;
                }

                break;
        }

        return false;
    }

    //攻击
    @Override
    public void attack() {

        //给敌人添加子弹
        Bullet bullet = new Bullet();
        //敌人子弹向下飞
        bullet.setDirection(1);


        if(this.type==0) {
            //敌人子弹速度
            bullet.setSpeed(10);

            //子弹图标
            bullet.setIcon(new ImageIcon("images/bullet/enemy_bullet3.png"));
        } else {
            //boss 子弹速度要快点
            bullet.setSpeed(15);
            bullet.setIcon(new ImageIcon("images/bullet/enemy_bullet1.png"));

        }

        //敌人子弹位置
        bullet.setX(this.getX()+this.getIcon().getIconWidth()/2-bullet.getIcon().getIconWidth()/2);
        bullet.setY(this.getY()+this.getIcon().getIconHeight()+bullet.getIcon().getIconHeight());



        //子弹状态
        bullet.setStatus(0);

        //开启子弹线程让子弹飞
        Thread thread = new Thread(bullet);
        thread.start();


        //往弹药库添加子弹
        this.getBullets().add(bullet);

    }



    @Override
    public void run() {

        while(true) {
            try {

                int direction = DataUtil.getAssignRangeRandom(1,4); //方向
                int step = DataUtil.getAssignRangeRandom(0,10); //步长
                //设置随机敌人速度
                switch (direction) {
                    case 1:
                        //上
                        for(int i=0;i<step;i++) {
                            this.moveUp();
                            Thread.sleep(50);
                        }
                        break;
                    case 2:
                        //下
                        for(int i=0;i<step;i++) {
                            this.moveDown();
                            Thread.sleep(50);
                        }
                        break;
                    case 3:
                        //左
                        for(int i=0;i<step;i++) {
                            this.moveLeft();
                            Thread.sleep(50);
                        }
                        break;
                    case 4:
                        //右
                        for(int i=0;i<step;i++) {
                            this.moveRight();
                            Thread.sleep(50);
                        }
                        break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
